package com.walterweimod.auxiliary_attributes.capability.impl;

import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeLivingEntityAttributeRangedValueBase;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraftforge.common.util.INBTSerializable;

import java.util.Objects;

public class BloodPoolValue extends ChargeLivingEntityAttributeRangedValueBase<Float>
        implements INBTSerializable<CompoundTag> {
    public static final String DESCRIPTION = "blood_pool_value";

    @Override
    public void updateMinMaxDefaultValue(){
        if (Objects.nonNull(getLivingEntity())){
            AttributeInstance max = getLivingEntity().getAttribute(getAttributeMap().get("max"));

            if (Objects.nonNull(max)){
                setMaxValue(
                        (float) max.getValue() * getLivingEntity().getMaxHealth()
                );
                setMinValue(0.0F);
                setDefaultValue(0.0F);
            }
        }
    }

    @Override
    public void addValue(Float value) {
        setValue(getValue() + value);
    }

    @Override
    public void clearValue() {
        setValue(0.0F);
        setMaxValue(0.0F);
        setMinValue(0.0F);
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag = new CompoundTag();
        tag.putFloat(DESCRIPTION, Objects.nonNull(getValue()) ? getValue() : 0.0F);
        return tag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        setValue(nbt.getFloat(DESCRIPTION));
    }
}
